package torres.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import javax.swing.JPanel;

import torres.entity.Entity;
import torres.entity.Merchant;
import torres.item.TakeableItem;
import torres.map.World;

@SuppressWarnings("serial")
public class PurchaseMenuView extends JPanel {
	
	int screenWidth, x, width;
	int screenHeight, y, height;
	TakeableItem item;
	Entity player;
	String text;
	HashMap<Integer, Dimension> actionMenuTable;
	
	public PurchaseMenuView() {
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		screenWidth = screenSize.width;
		screenHeight = screenSize.height;
		
		x = screenWidth/6;
		y = 5*screenHeight/6;
		
		setIgnoreRepaint(true);
		setPreferredSize(new Dimension(2*screenWidth/3, screenHeight/15));
		setLocation(x, y);
		setLayout(null);
		setVisible(true);
		setFocusable(true);
		setOpaque(true);
		
		player = World.getInstance().getPlayer();
	}
	
	public void paintComponent(Graphics g) {
		
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		
		g.setColor(Color.black);
		drawWindow(g);
		
		//int yOffset = 30;
		//int xOffset = 20;
		
		Font body = new Font("Helvetica",Font.BOLD, 22);
		
		g.setFont(body);
		g.setColor(Color.black);
		drawActions(g2);
	}
	
	public void drawWindow(Graphics g) {
		ImageLoader loader = new ImageLoader("texture.jpg");
		BufferedImage img = loader.getImage();
//		Direction dir = player.getDirection();
		if(x!=0&&y!=0) g.drawImage(img,x,y,2*screenWidth/3,screenHeight/15,null);
	}
	
	public void drawActions(Graphics2D g2) {
		//System.out.println("Drawing at "+x+" "+y);
		g2.drawImage(item.getImage(), x,y-screenHeight/50,null);
		if(text.equals("BUY"))
			g2.drawString("PRICE: "+((Merchant)World.getInstance().getPlayer().getInteractedWith()).SellPrice(item, World.getInstance().getPlayer()), x+screenWidth/10,y+screenHeight/20);
		else
			g2.drawString("PRICE: "+((Merchant)World.getInstance().getPlayer().getInteractedWith()).BuyPrice(item), x+screenWidth/10,y+screenHeight/20);
		g2.draw3DRect(x+screenWidth/2,y, x, y-64*screenHeight/83, true);
		g2.drawString(text, x+4*screenWidth/7,y+screenHeight/20);
	}
	
	public void update(Graphics g) {
		update(g, item, text);
	}
	
	public void update(Graphics g, TakeableItem t, String text) {
		item = t;
		this.text = text;
		paintComponent(g);
	}
	
	public HashMap<Integer,Dimension> getActionMenuTable() {
		return actionMenuTable;
	}
	
	public int getWidth() {
		return x;
	}
	
	public int getHeight() {
		return y-64*screenHeight/83;
	}
	
	public int getX() {
		return x+screenWidth/2;
	}
	
	public int getY() {
		return y;
	}
	
	public String getText() {
		return text;
	}
	
	public TakeableItem getItem() {
		return item;
	}
}
